package com.yksj.healthtalk.ui.home;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.entity.MyZoneGroupEntity;
import com.yksj.healthtalk.ui.R;


public class ShopClassifyChooseActivity extends BaseFragmentActivity implements OnClickListener,OnItemClickListener{
	
	private List<MyZoneGroupEntity> entities;
	private ListView listView;
	private ShopClassAdapter adapter;
	private String isselect;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.shop_classify_choose_main);
		entities = getIntent().getParcelableArrayListExtra("data");
		isselect = getIntent().getStringExtra("position");
		initView();
	}
	
	public void initView() {
		initTitle();
		adapter = new ShopClassAdapter(this,entities.subList(1, entities.size()));
		listView  = (ListView)findViewById(R.id.listview);
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(this);
		titleLeftBtn.setOnClickListener(this);
		titleTextV.setText("摄影作品展示分类");
		titleTextV.setTextColor(Color.BLACK);
	}
	
	private class ShopClassAdapter extends BaseAdapter {
		
		private LayoutInflater inflater;
		private List<MyZoneGroupEntity> data;
		
		public ShopClassAdapter(Context context,List<MyZoneGroupEntity> data) {
			inflater = LayoutInflater.from(context);
			this.data = data;
		}
		
		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return data.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if(convertView==null) {
				convertView = inflater.inflate(R.layout.shop_classify_item, parent,false);
				holder = new ViewHolder();
				holder.mImageView = (ImageView) convertView.findViewById(R.id.class_choose_tag);
				holder.mTextView = (TextView) convertView.findViewById(R.id.class_choose);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			
			if(position==0&&position==this.getCount()-1){
				convertView.setBackgroundResource(R.drawable.text_background_single);
			}else if(position==this.getCount()-1){
				convertView.setBackgroundResource(R.drawable.textview_background_down);
			} else if(position == 0) {
				convertView.setBackgroundResource(R.drawable.textview_background_up);
			}
			
			MyZoneGroupEntity entity  = entities.get(position);
			if(entity.getClassName().equals(isselect)) {
				holder.mImageView.setVisibility(View.VISIBLE);
			} else {
				holder.mImageView.setVisibility(View.INVISIBLE);
			}
			holder.mTextView.setText(entity.getClassName());
			return convertView;
		}
		
		private class ViewHolder {
			TextView mTextView;
			ImageView mImageView;
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			finish();
			overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		Intent intent = getIntent();
		intent.putExtra("ClassName", entities.get(position).getClassName());
		intent.putExtra("ClassId", entities.get(position).getClassId());
		setResult(RESULT_OK, intent);
		finish();
		overridePendingTransition(R.anim.back_enter,R.anim.back_exit);
	}
}
